﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace SerialPort_URAT
{
    class LEV_D2_DataFormatParsingClass
    {
        private string sDefParsingFileFolder = "ini";

        private string sDefVersionOffsetFileName = "LevPasingVersion.ini";   //define version offset
        private string sDefPasingRefFileName = "LevPasingRefFileList.ini";   //Linking with Version and ParsingFile
        private string gSystemInfoParsingFileName;   //System Information Parsing List
        private string gRomInfoParsingFileName;   //EEPROM Information Parsing List


        private string sDefMajorVerString = "MajorVer_Offset";
        private string sDefMinorVerString = "MinorVer_Offset";
        private string sDefRomVerString = "RomVer_Offset";
        private string sDefReservedVerString = "ReservedVer_Offset";
        private int gMajorVer_Offset;
        private int gMinorVer_Offset;
        private int gRomVer_Offset;
        private int gReservedVer_Offset;



        private Assembly asm;
        private string gCurrentProjectDirectory;

        public delegate void ShowLogMessage(string str);
        public ShowLogMessage Log;
        public LEV_D2_DataFormatParsingClass(ShowLogMessage log)
        {
            Log = log;
            //asm = Assembly.GetExecutingAssembly();
            //ProjectName = asm.GetName().Name;
            //Stream AsmStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ProjectName + "." + VersionOffsetFileName);
            List<string> Valid_Line_String_List;
            gCurrentProjectDirectory = System.Windows.Forms.Application.StartupPath;

            string subPathFileString = Path.Combine(sDefParsingFileFolder, sDefVersionOffsetFileName);

            Valid_Line_String_List = getValid_Lines_From_infoFile(subPathFileString);
            VersionOffsetDataFormatParsing(Valid_Line_String_List);

            ////////////////////////////////////////////
            subPathFileString = Path.Combine(sDefParsingFileFolder, sDefPasingRefFileName);
            ReferenceFileListParsing(subPathFileString);
 
        }
        private List<string> getValid_Lines_From_infoFile(string subPathFileName)
        {
            List<string> valid_Line_String_List;
            valid_Line_String_List = new List<string>();

            gCurrentProjectDirectory = System.Windows.Forms.Application.StartupPath;
            string pathFileString = Path.Combine(gCurrentProjectDirectory, subPathFileName);
            //if (!System.IO.Directory.Exists(pathFileString))


            if (!System.IO.File.Exists(pathFileString))
            {
                Log("[ParsingClass]Logfile cannot be read");
                return new List<string>();
                //throw new System.InvalidOperationException("Logfile cannot be read");
            }
            else
            {
                Log("[ParsingClass]Logfile exists");
            }

            try
            {
                FileStream fsFile = new FileStream(pathFileString, FileMode.Open);
                /*
                 * 讀取數據最簡單的方法是Read()。此方法將流的下一個字符作為正整數值返回，
                 * 如果達到了流的結尾處，則返回-1。
                 */
                StreamReader srReader = new StreamReader(fsFile, Encoding.Default);
                ////////////////////////////////////////////////////////////////////////
                //Continue to read until you reach end of file
                //Find Start Address
                string str;
                while (srReader.Peek() >= 0)
                {
                    str = srReader.ReadLine();
                    str = HM_Utilitys.StringTrim_Replace_EnterAndTab_CombineMultiSpaceToOne(str);
                    if (str.StartsWith("#") || (str == string.Empty))
                    {
                        continue;
                    }
                    else
                    {
                        //Log(str);
                        valid_Line_String_List.Add(str);
                    }
                }
                //Log(valid_Line_String_List.Count.ToString());
                //VersionOffsetDataFormatParsing(Valid_Line_String_List);
                srReader.Close();
                fsFile.Close();
            }
            catch (Exception ex)
            {
                Log("[ParsingClass]Open File Fail : " + pathFileString);
            }
            return valid_Line_String_List;
        }
        private void VersionOffsetDataFormatParsing(List<string> valid_Line_String_List)
        {
            string[] strArr;
            try
            {
                foreach (string str in valid_Line_String_List)
                {
                    if (str.StartsWith(sDefMajorVerString))
                    {
                        strArr = str.Split(new char[] { '=' });
                        gMajorVer_Offset = Convert.ToInt16(strArr[1]);
                        Log("[ParsingClass]MajorVer_Offset = " + gMajorVer_Offset.ToString());
                    }
                    else if (str.StartsWith(sDefMinorVerString))
                    {
                        strArr = str.Split(new char[] { '=' });
                        gMinorVer_Offset = Convert.ToInt16(strArr[1]);
                        Log("[ParsingClass]MinorVer_Offset = " + gMinorVer_Offset.ToString());
                    }
                    else if (str.StartsWith(sDefRomVerString))
                    {
                        strArr = str.Split(new char[] { '=' });
                        gRomVer_Offset = Convert.ToInt16(strArr[1]);
                        Log("[ParsingClass]RomVer_Offset = " + gRomVer_Offset.ToString());
                    }
                    else if (str.StartsWith(sDefReservedVerString))
                    {
                        strArr = str.Split(new char[] { '=' });
                        gReservedVer_Offset = Convert.ToInt16(strArr[1]);
                        Log("[ParsingClass]ReservedVer_Offset = " + gReservedVer_Offset.ToString());
                    }
                }
            }
            catch(Exception ex)
            {
                Log("[ParsingClass]Convert Fail : " + ex);
            }
        }

        private List<string> getDefinedSectionLines(string defSectionString, List<string> input_ValidLineStringList)
        {
            return new List<string>();
        }

        private void ReferenceFileListParsing(string subPathFileName)
        {
            string sDefSectionDefine = "[defaultReference]";   //
            string sDefSectionSysInfo = "[SysInfoReference]";   //
            string sDefSectionRomInfo = "[RomInfoReference]";   //

            string sDefMajorVerString = "MajorVer";
            string sDefMinorVerString = "MinorVer";
            string sDefRomVerString = "romVersion";

            string sDefRefSystemInfoFileString = "refSystemInfoFile";
            string sDefRefRomInfoFileString = "refRomInfoFile";

            List<string> valid_Line_String_List;
            valid_Line_String_List = getValid_Lines_From_infoFile(subPathFileName);


            List<string> systemInfoVersionValuesMinList = new List<string>();
            List<string> systemInfoVersionValuesMaxList = new List<string>();
            List<string> systemInfoReferenceFileList = new List<string>();

            bool save = false;
            foreach (string str in valid_Line_String_List)
            {
                if(str.StartsWith(sDefSectionSysInfo)){
                }

            }


        }
    }//class LEV_D2_DataFormatParsingClass

    class LEV_D2_ReferenceFileClass
    {
        string sDefSectionDefine = "[defaultReference]";   //
        string sDefSectionSysInfo = "[SysInfoReference]";   //
        string sDefSectionRomInfo = "[RomInfoReference]";   //

        string sDefMajorVerString = "MajorVer";
        string sDefMinorVerString = "MinorVer";
        string sDefRomVerString = "romVersion";

        string sDefRefSystemInfoFileString = "refSystemInfoFile";
        string sDefRefRomInfoFileString = "refRomInfoFile";
        

        public LEV_D2_ReferenceFileClass(List<string> contentLineList)
        {
            List<string> systemInfoVersionValuesMinList = new List<string>();
            List<string> systemInfoVersionValuesMaxList = new List<string>();
            List<string> systemInfoReferenceFileList = new List<string>();
            foreach (string str in contentLineList)
            {

            }

        }
    }//class LEV_D2_ReferenceFileClass


}
